<template>
  <div class="search-container">
    <!-- 搜索栏》事件监听 -->
    <form action="/" class="search">
      <van-search
        v-model.trim="searchtext"
        show-action
        background="#fff"
        placeholder="搜索产品"
      />
    </form>

    <!-- 搜索结果 -->
    <!-- 没有搜索结果时为false不显示 只有有结果时才显示 -->
    <!-- <Search-result
    v-if="SearchResult"
    :searchtext="searchtext" /> -->
    <!-- /搜索结果 -->

    <!-- 联想建议 -->
    <!-- 联想输入框没有时不显示，只有有内容时才显示 -->
    <!-- <Search-suggestion
      v-else-if="searchtext"
      :searchtext="searchtext"
      @search="onSearch"
    /> -->
    <!-- /联想建议 -->

    <!-- 历史记录 -->
    <!-- <Search-history v-else
    :Searchhistorys='Searchhistorys'
    :delete.sync = Searchhistorys
    @search="onSearch"
    /> -->
    <!-- /历史记录 -->
  </div>
</template>

<script>
// import SearchHistory from './components/search-history'
// import SearchSuggestion from './components/search-suggestion'
// import SearchResult from './components/search-result'
// import { setItem, getItem } from '@/utils/storage.js'
export default {
  name: 'search-container',
  // components: {
  //   SearchHistory,
  //   SearchSuggestion,
  //   SearchResult
  // },
  data () {
    return {
      searchtext: '',
      SearchResult: false
      // 从本地获取历史记录
      // Searchhistorys: getItem('Search_historys') || []
    }
  },
  // 监听输入框的改变同步保存到本地
  // watch: {
  //   Searchhistorys (value) {
  //     setItem('Search_historys', value)
  //   }
  // },
  methods: {
    // 搜索功能
    onSearch (val) {
      console.log(val)
      const index = this.Searchhistorys.findIndex((v) => v === val)
      console.log(index)
      if (index !== -1) {
        this.Searchhistorys.splice(index, 1)
      }
      this.Searchhistorys.unshift(val)
      this.searchtext = val
      this.SearchResult = true
    },
    onCancel () {
      this.$router.back()
    }
  }
}
</script>

<style scoped lang="less">
.search-container {
  height: 50px;
  .van-search__action {
    color: #fff;
  }
  .search {
    padding-top: 106px;
    .van-search {
      position: fixed;
      top: 0;
      left: 0;
      right: 0;
      z-index: 1;
    }
    .van-search__action {
      color: #323233;
    }
  }
}
</style>
